#include<bits/stdc++.h>
using namespace std;
int abb(int a){
if(a<0)return 1;
else return a;
}

struct p{
int i;
int d;
};
bool cmp(p a,p b){
    return a.d<b.d;
}
int main(){
    freopen("detect.in","r",stdin);
    freopen("detect.out","w",stdout);
    int t;
    cin>>t;
    while(t>0){
        int a[100010]={0};
        int n,m,ln,vl;
        cin>>n;
        for(int i=1;i<=n;i++){
        int p,a1,v,l,r;
        cin>>p>>v>>a1;
        if(a1>0){
            if(vl>v)
            l=p+ceil(abb(vl-v)/1.0/a1);
            else l=p;
            r=ln;
        }
        if(a1=0){
            if(v<=vl)continue;
            else{
               l=p;
               r=ln;
            }
        }
        if(a1<0){
            if(v<=vl)continue;
            else{
                l=p;
                r=p+ceil((v-vl)/1.0/a1);
            }
        }
        for(int i=l;i<=r;i++)a[i]++;
        }
        int b[100010];
        vector<p>v;
        int ans=0;
        for(int i=1;i<=m;i++){
            cin>>b[i];
            if(a[b[i]]==0){
                ans++;
                continue;
            }
            p w;
            w.i=b[i];
            w.d=a[b[i]];
            v.push_back(w);
            sort(v.begin(),v.end(),cmp);
        }
        for(int i=1;i<=m;i++){
                if(a[v[i].i]<=0){
                    ans++;
                    continue;
                }
                int k=v[i].i;
                int j=v[i].d;
            while((a[k]<v[i].d||a[k]!=0)&&(a[j]<v[i].d||a[j]!=0)){
                if(a[k]<v[i].d||a[k]!=0)k++;
                if(a[j]<v[i].d||a[j]!=0)j--;
                a[k]-=v[i].d;
                a[j]-=v[i].d;
            }
        }
        cout<<ans;
    }
    return 0;
    }
